<?php
class Controller_BoHome extends Controller_Boprepare
{
    /**
     * 后台登陆首页
     * 显示欢迎界面
     */
    public function actionIndex()
    {
        $this->_view['notice']  = '欢迎登录';
        $this->_view['cssfile'] = 'BoHome.css';
    }

    /**
     * 更改登陆密码
     * 教师专用 {$_SESSION['lrole_id'] == 4 }
     *
     * 其他帐号不可更改密码
     */
    public function actionChangePassword()
    {
        $notice = '修改密码';

        if(!empty($_POST) && $_SESSION['lrole_id'] == 4)
        {
            $teacherObj = Login::find('teacher_id = ?', $_SESSION['tid'])->query();
            $teacherObj->cib_pwd = $_POST['new_pwd'];
            $teacherObj->save();
            $notice = '密码已修改 欢迎登录！';
        }

        $this->_view['notice']  = $notice;
        $this->_view['cssfile'] = 'BoHome.css';
    }

    public function actionEditPictureNews()
    {
        $Phome_pic_news_id   = isset($_POST['home_pic_news_id'])   ? $_POST['home_pic_news_id']   : NULL;
        $Phome_pic_news_text = isset($_POST['home_pic_news_text']) ? $_POST['home_pic_news_text'] : NULL;
        $Fpicture = isset($_FILES['picture']) ? $_FILES['picture'] : NULL;

        if(News::find('news_id = ?', $Phome_pic_news_id)->query()->news_id != '')
        {
            $pnid = AppStore::find('hkey = ?', 'home_pic_news_id')->query();
            $pnid->hvalue = $Phome_pic_news_id;
            $pnid->save();

            $pntxt = AppStore::find('hkey = ?', 'home_pic_news_text')->query();
            $pntxt->hvalue = $Phome_pic_news_text;
            $pntxt->save();
        }

        /**
         * 如果有新图片上传，处理图片
         * 有内容"提交"后
         * 如果有附件，处理附件，更新 $attachment
         */
        if($Fpicture && $Fpicture['error'] != 4)
        {
            // 删除旧头像
            unlink('misc/home-index.jpg');
            // 添加新头像
            $uploader = new Helper_Uploader();
            $uploader->file('picture')->move('misc/home-index.jpg');
        }

        $this->_view['pictureNewsId']   = AppStore::find('hkey = ?', 'home_pic_news_id')  ->query()->hvalue;
        $this->_view['pictureNewsText'] = AppStore::find('hkey = ?', 'home_pic_news_text')->query()->hvalue;
    }
}